#ifndef __VERHICLE__
#define __VERHICLE__

#include "GameObject.h"
#include "Header.h"

#define LOG_LABEL_ENABLE 0

enum  GAME_ID
{
    ID_BUS,
    ID_CAR,
    ID_TRUCK,
    ID_SMALL_MOTO,
    ID_BIG_MOTO,
};

enum HOR_DIR
{
	HD_NONE,
	HD_LEFT,
	HD_RIGHT
};

enum VehicleStateEnum {
    kVehicleStateRunning,
    kVehicleStateStop,
    kVehicleStateBraking,
};

class Road;

class Vehicle : public GameObject
{
public:
    
    float expectV;
    float expectA;
    float expectDecreaseA;
    float distanceWhenWaitingRedLight;
    float vChangeLane;
    float vPassBus;
    bool isChangeLane;
    
    
	Vehicle();
	~Vehicle();
	bool init();

	// near range is the range which vehicles can interact together. User this to limit number vehicles check with this vehicle, to improve performance
	bool isInInteractableRange(Vehicle* vehicle);
	float calculateVTurn();

	void setRoad(Road* road);
	CCRect getSafeRect();
	CCPoint getV();
	CCRect getLeftSafeRectBeside();
	CCRect getRightSafeRectBeside();
	CCRect getLeftSafeRectNextLane();
	CCRect getRightSafeRectNextLane();
	CCRect getSafeRectBeside();
	int getVehicleId();
	int getNumTurn();
	bool isBesideSafe();
	void updatePosition(float delta);
    void readInfo(VehicleData* vehicleData);
	bool isSafe;
    float getSafeDistanceAhead();
    void speedUp();
	void changeState(int state);
    void stop();
    void brake(float fA);

protected:
	int vehicleId;
	CCPoint v;
    CCPoint a;
    
	Road* road;
	CCRect safeRectAhead;	// is safe in along vehicle, use to check to change sub road
    CCRect safeRectAheadWaitRedLight; // is safe in along vehicle, when wait red light
	CCRect safeRectBeside;			// NOT USE
#if LOG_LABEL_ENABLE
    CCLabelTTF* logLabel;
    char logString[64];
#endif
	bool isEnableTurn;
	int numTurn;

	// rect to check be able to turn left and right
	CCRect leftSafeRectBeside;
	CCRect rightSafeRectBeside;

	CCRect leftSafeRectNextLane;
	CCRect rightSafeRectNextLane;

	int horizontalDirection;
	float horizontalTarget;

	// for save lane info
	bool isInLane1;
	bool isInLane2;
	bool isInLane3;

	void update(float delta);
	void checkTurnDone();
	void checkResignRoadForBus(Vehicle* vehicle);
	virtual void noTurn();
	void checkSafeWithAheadVehicles(CCPoint oldPosition);
	void calculateSafeRect();
	void checkTrafficLight();
	void calculateSafeRectBeside();
	void draw();
#if LOG_LABEL_ENABLE
    void updateLogString();
#endif
    Vehicle* getNearestVehicleAheadSameLane();
    float getDistanceToStopWhenRedLight();
    bool isWaitingRedLight();
};

#endif // !VERHICLE
